package com.coderdream.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

public class DateUtil {
	/**
	 * 获取前/后n天日期(M月d日)
	 * 
	 * @return
	 */
	public static String getMonthDayMMdd(int diff) {
		DateFormat df = new SimpleDateFormat("MMdd");
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_YEAR, diff);
		return df.format(c.getTime());
	}

	public static String printMonthDay() {
		StringBuffer sb = new StringBuffer("");
		for (int i = 1; i < 13; i++) {
			switch (i) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				for (int j = 1; j < 32; j++) {
					sb.append(i + "月" + j + "日\n");
				}
				break;
			case 2:
				for (int j = 1; j < 30; j++) {
					sb.append(i + "月" + j + "日\n");
				}
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				for (int j = 1; j < 31; j++) {
					sb.append(i + "月" + j + "日\n");
				}
				break;

			default:
				break;
			}
			sb.append("\n\n");
		}

		return sb.toString();
	}

	public static List<String> getMonthDayList() {
		List<String> list = new ArrayList<String>();
		for (int i = 1; i < 13; i++) {
			switch (i) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				for (int j = 1; j < 32; j++) {
					list.add(i + "月" + j + "日");
				}
				break;
			case 2:
				for (int j = 1; j < 30; j++) {
					list.add(i + "月" + j + "日");
				}
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				for (int j = 1; j < 31; j++) {
					list.add(i + "月" + j + "日");
				}
				break;

			default:
				break;
			}
		}

		return list;
	}

	public static Map<String, String> getMonthDayMap() {
		Map<String, String> map = new TreeMap<String, String>();
		for (int i = 1; i < 13; i++) {
			switch (i) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				for (int j = 1; j < 32; j++) {
					map.put(i + "月" + j + "日", i + "月" + j + "日");
				}
				break;
			case 2:
				for (int j = 1; j < 30; j++) {
					map.put(i + "月" + j + "日", i + "月" + j + "日");
				}
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				for (int j = 1; j < 31; j++) {
					map.put(i + "月" + j + "日", i + "月" + j + "日");
				}
				break;

			default:
				break;
			}
		}

		return map;
	}

	public static Set<String> getMonthDaySet() {
		Set<String> set = new TreeSet<String>();
		for (int i = 1; i < 13; i++) {
			switch (i) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				for (int j = 1; j < 32; j++) {
					set.add(i + "月" + j + "日");
				}
				break;
			case 2:
				for (int j = 1; j < 30; j++) {
					set.add(i + "月" + j + "日");
				}
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				for (int j = 1; j < 31; j++) {
					set.add(i + "月" + j + "日");
				}
				break;

			default:
				break;
			}
		}

		return set;
	}

	/**
	 * @param dayString
	 * @return
	 */
	public static String transferDayToNumberString(String dayString) {
		String result = "";
		int index1 = dayString.indexOf("月");
		int index2 = dayString.indexOf("日");

		int month = Integer.valueOf(dayString.substring(0, index1));
		int day = Integer.valueOf(dayString.substring(index1 + 1, index2));
		String monthStr = String.valueOf(month);
		if (month < 10) {
			monthStr = "0" + String.valueOf(month);
		}
		String dayStr = String.valueOf(day);
		if (day < 10) {
			dayStr = "0" + String.valueOf(day);
		}

		result = monthStr + dayStr;

		// System.out.println(result);
		return result;
	}

	public static void main(String[] args) {
		// System.out.println(printMonthDay());

		// List<String> list = getMonthDayList();
		// for (Iterator<String> iterator = list.iterator();
		// iterator.hasNext();) {
		// String string = iterator.next();
		// System.out.println(string);
		// }

		transferDayToNumberString("1月4日");
		transferDayToNumberString("1月14日");
		transferDayToNumberString("12月4日");
		transferDayToNumberString("12月14日");
	}
}
